/*
Source : https://leetcode.com/problems/longest-common-prefix/
Author : nflush@outlook.com
Date   : 2016-06-27
*/
/*
14. Longest Common Prefix
Total Accepted: 106941 Total Submissions: 371499 Difficulty: Easy

Write a function to find the longest common prefix string amongst an array of strings.

Subscribe to see which companies asked this question

*/

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if (strs.size() == 0){
            return string("");
        }
        string com = strs[0];
        int len = com.size();
        for (int i =1; i<strs.size();i++){
            getCom(com, strs[i], len);
        }
        return com.substr(0, len);
    }
private:
    void getCom(const string &a, const string &b, int &len){
        int i =0;
        int max = len < b.size()?len:b.size();
        while (i<max && a[i] == b[i]){
            i++;
        }
        len = i;
    }
};
